home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / libc / bcmp.c < prev    next >
C/C++ Source or Header  |  1991-03-24  |  3KB  |  97 lines

  1. /* 
  2.  * bcmp.c --
  3.  *
  4.  *    Source code for the "bcmp" library routine.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/bstring/RCS/bcmp.c,v 1.3 91/03/24 19:02:17 kupfer Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. /*
  21.  * The following mask is used to detect proper alignment of addresses
  22.  * for doing word operations instead of byte operations.  It is
  23.  * machine-dependent.  If none of the following bits are set in an
  24.  * address, then word-based operations may be used. This value is imported
  25.  * from machparam.h
  26.  */
  27.  
  28. #include "machparam.h"
  29.  
  30. #define WORDMASK WORD_ALIGN_MASK
  31.  
  32. #ifdef KERNEL
  33. #include <vmHack.h>
  34. #endif
  35.  
  36. /*
  37.  *----------------------------------------------------------------------
  38.  *
  39.  * bcmp --
  40.  *
  41.  *    Compare two blocks of memory for equality.  This routine is
  42.  *    optimized to do integer compares.  However, if either sourcePtr
  43.  *    or destPtr points to non-word-aligned addresses then it is
  44.  *    forced to do single-byte compares.
  45.  *
  46.  * Results:
  47.  *    The return value is zero if the blocks at sourcePtr and destPtr
  48.  *    are identical, non-zero if they differ.
  49.  *
  50.  * Side effects:
  51.  *    None.
  52.  *
  53.  *----------------------------------------------------------------------
  54.  */
  55.  
  56. int
  57. bcmp(sourcePtr, destPtr, numBytes)
  58.     register char *sourcePtr;        /* Where to compare from */
  59.     register char *destPtr;        /* Where to compare to */
  60.     register int numBytes;        /* The number of bytes to compare */
  61. {
  62. #ifdef VM_CHECK_BSTRING_ACCESS
  63.     Vm_CheckAccessible(sourcePtr, numBytes);
  64.     Vm_CheckAccessible(destPtr, numBytes);
  65. #endif
  66.     /*
  67.      * If both the sourcePtr and the destPtr point to aligned addesses then
  68.      * compare as much as we can in integer units.  Once we have less than
  69.      * a whole int to compare then it must be done by byte compares.
  70.      */
  71.  
  72.     if ((((int) sourcePtr & WORDMASK) == 0)
  73.         && (((int) destPtr & WORDMASK) == 0)) {
  74.     while (numBytes >= sizeof(int)) {
  75.         if (*(int *) destPtr != *(int *) sourcePtr) {
  76.         return 1;
  77.         }
  78.         sourcePtr += sizeof(int);
  79.         destPtr += sizeof(int);
  80.         numBytes -= sizeof(int);
  81.     }
  82.     }
  83.  
  84.     /*
  85.      * Compare the remaining bytes
  86.      */
  87.  
  88.     while (numBytes > 0) {
  89.     if (*destPtr++ != *sourcePtr++) {
  90.         return 1;
  91.     }
  92.     numBytes--;
  93.     }
  94.  
  95.     return 0;
  96. }
  97.